﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Array2DExtensions
{
    /// <summary>
    /// Represents the coordinates of a cell in a 2D array
    /// Stanislav Radkov®  2012 :D 
    /// </summary>
    public partial class Index
    {
        public Int32 Row { get; set; }
        public Int32 Col { get; set; }

        #region Constructors

        public Index(Int32 row, Int32 col)
        {
            this.Row = row;
            this.Col = col;
        }

        public Index() : this(0, 0)
        {
        }

        public static Index operator +(Index a, Index b)
        {
            Index result = new Index();
            result.Row = a.Row + b.Row;
            result.Col = a.Col + b.Col;

            return result;
        }

        public static Index operator -(Index a, Index b)
        {
            Index result = new Index();
            result.Row = a.Row - b.Row;
            result.Col = a.Col - b.Col;

            return result;
        }

        #endregion

        #region Public Methods

        public static bool operator ==(Index a, Index b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(Index a, Index b)
        {
            return !a.Equals(b);
        }

        public override bool Equals(object obj)
        {
            if (obj == null) 
                return false;

            if (this.GetType() != obj.GetType()) 
                return false;

            Index otherIndex = (Index)obj;

            bool rowAreEqual = this.Row == otherIndex.Row;
            bool colsAreEqual = this.Col == otherIndex.Col;

            return rowAreEqual && colsAreEqual;
        }

        public override string ToString()
        {
            return String.Format("Row:{0} Col:{1}", this.Row, this.Col);
        }
        #endregion
    }

    /// <summary>
    /// Represents the 4 directions
    /// Stanislav Radkov®  2012 :D 
    /// </summary>
    public partial class Index
    {
        //We assume top-left is [0,0]
        public static readonly Index Up = new Index(-1, 0);
        public static readonly Index Down = new Index(1, 0);
        public static readonly Index Left = new Index(0, -1);
        public static readonly Index Right = new Index(0, 1);

        public static readonly List<Index> StraightDirections = new List<Index>() { Up, Down, Left, Right };
        public static readonly List<Index> StraightAndDiagonalDirections = new List<Index>() { Up, Down, Left, Right, Left + Up, Up + Right, Right + Down, Down + Left };
    }
}
